package com.example.shop_car_2208.mvvmdemo

import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import com.blankj.utilcode.util.ToastUtils
import com.example.shop_car_2208.MainActivity
import com.example.shop_car_2208.R

class LoginActivity : AppCompatActivity() {
    val vm  = LoginViewModel()
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        val username = findViewById<EditText>(R.id.username)
        val password = findViewById<EditText>(R.id.password)
        val btn = findViewById<Button>(R.id.btn)

        btn.setOnClickListener {
            vm.login(username.text.toString(), password.text.toString())
        }
        vm.loginSuccess.observe(this){
            ToastUtils.showLong(it.message)
            val intent = Intent(this, MainActivity::class.java)
            // 启动活动
            startActivity(intent)
        }
        vm.loginFail.observe(this){
            ToastUtils.showLong(it)
        }
    }
}